import 'package:flutter/material.dart';
import 'package:flutter_rider/api/api.dart';
import 'package:flutter_rider/config.dart';
import 'package:flutter_rider/kit/loc_kit.dart';
import 'package:flutter_rider/module/mine/api/user_api.dart';
import 'package:flutter_rider/utils/log.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';

///是否休息中
final isWorkProvider = StateNotifierProvider<WorkProvider, bool>((ref) {
  return WorkProvider();
});

final isWorkListenProvider = Provider((ref) {
  //这里要判断它是否是上班状态才开始定位，休息不上报经纬度
  ref.listen(isWorkProvider, (previous, next) {
    Log.i(
        "这里要判断它是否是上班状态才开始定位，休息不上报经纬度 previous---->$previous  next ------>$next");
    if (next as bool) {
      //上班
      LocKit.instance.startLocation();
    } else {
      //休息
      LocKit.instance.stopLocation();
    }
  });
});

class WorkProvider extends StateNotifier<bool> {
  WorkProvider() : super(false); //默认休息中

  setWork(BuildContext context, bool isWork) async {
    if (state == isWork) return; //没有变化就不执行请求了
    var json = {};
    json["on"] = isWork;
    Api.submitx(context, UserApi.duty, data: json, success: ((data) {
      state = isWork;
    }));
  }

  getWork() {
    Api.get(UserApi.duty, (data, response) {
      if (data != null) {
        state = data;
      }
    });
  }
}


///是否有查看操作员权限
final isShowOperatorProvider = StateProvider<bool>((ref) {
  return false;
});
